package com.dms.modules.product.controller;

import com.dms.common.api.ApiResponse;
import com.dms.common.security.SecurityUtils;
import com.dms.modules.product.service.ProductRecommendationService;
import com.dms.modules.product.vo.ProductDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import java.util.List;

@Slf4j
@Validated
@RestController
@RequestMapping("/api/v1/products/recommendations")
@Api(tags = "商品推荐接口")
@PreAuthorize("hasRole('USER')")
public class RecommendationController extends BaseProductController {

    @Autowired
    private ProductRecommendationService recommendationService;

    @GetMapping("/personalized")
    @ApiOperation("获取个性化推荐商品")
    @PreAuthorize("isAuthenticated()")
    public ApiResponse<List<ProductDetailVO>> getPersonalizedRecommendations(
            @RequestParam(defaultValue = "10") @Min(value = 1, message = "推荐数量不能小于1") Integer limit) {
        Long userId = getUserId();
        return ApiResponse.success(recommendationService.getPersonalizedRecommendations(userId, limit));
    }

    @GetMapping("/browse-history")
    @ApiOperation("获取基于浏览历史的推荐商品")
    @PreAuthorize("isAuthenticated()")
    public ApiResponse<List<ProductDetailVO>> getBrowseHistoryRecommendations(
            @RequestParam(defaultValue = "10") @Min(value = 1, message = "推荐数量不能小于1") Integer limit) {
        Long userId = getUserId();
        return ApiResponse.success(recommendationService.getBrowseHistoryRecommendations(userId, limit));
    }

    @GetMapping("/favorites")
    @ApiOperation("获取基于收藏的推荐商品")
    @PreAuthorize("isAuthenticated()")
    public ApiResponse<List<ProductDetailVO>> getFavoriteRecommendations(
            @RequestParam(defaultValue = "10") @Min(value = 1, message = "推荐数量不能小于1") Integer limit) {
        Long userId = getUserId();
        return ApiResponse.success(recommendationService.getFavoriteRecommendations(userId, limit));
    }

    @GetMapping("/search-history")
    @ApiOperation("获取基于搜索历史的推荐商品")
    @PreAuthorize("isAuthenticated()")
    public ApiResponse<List<ProductDetailVO>> getSearchHistoryRecommendations(
            @RequestParam(defaultValue = "10") @Min(value = 1, message = "推荐数量不能小于1") Integer limit) {
        Long userId = getUserId();
        List<ProductDetailVO> recommendations = recommendationService.getSearchHistoryRecommendations(userId, limit);
        return ApiResponse.success(recommendations);
    }
} 